package com.example.mydemo;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import java.util.Timer;
import java.util.TimerTask;
import android.os.Handler;
import android.widget.Button;

public class SmartRestartService extends IntentService {
    private static final long TIMEOUT_MILLS = 5 * 1000L;
    private Handler mHandler;
    AlertDialog alertDialog;
    Dialog dialog;
    public SmartRestartService() {
        super("SmartRestartService");
    }

    public SmartRestartService(String name) {
        super(name);
    }

    private void justShowDialog() {
        final View dialogView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_layout, null);
        Button cancelButton = dialogView.findViewById(R.id.cancel);
        Button okButton = dialogView.findViewById(R.id.okay);
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("lfy", "[SmartRestartService] 点击了确定按钮...");
            }
        });
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("lfy", "[SmartRestartService] 点击了取消按钮...");
            }
        });
        Log.d("lfy", "[SmartRestartService] showDialog...cancelButton="+cancelButton+",okButton="+okButton);

        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext())
                .setView(dialogView);
        alertDialog = builder.create();
        //设置点击其他地方不可取消此 Dialog
        alertDialog.setCancelable(false);
        alertDialog.setCanceledOnTouchOutside(false);


        Log.d("lfy", "[SmartRestartService] showDialog...444...");
        final WindowManager.LayoutParams params = alertDialog.getWindow().getAttributes();
        params.width = 600;
        params.height = 200;
        alertDialog.getWindow().setAttributes(params);
        alertDialog.getWindow().getDecorView().setPadding(50, 30, 50, 30);

        //8.0系统加强后台管理，禁止在其他应用和窗口弹提醒弹窗，如果要弹，必须使用TYPE_APPLICATION_OVERLAY，否则弹不出
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            alertDialog.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
        }else {
            alertDialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
        }
        alertDialog.show();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String action = intent.getAction();
        android.util.Log.d("lfy","[SmartRestartService] onHandleIntent...进入服务了...action="+action);
        if (action.equals("android.service.action.SMARTRESTART")){
            mHandler = new Handler(Looper.getMainLooper());
            //在子线程中想要 Toast 的地方添加如下
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    justShowDialog();
                }
            });
            //显示弹窗，然后弹出提示语，倒计时１５秒
            Timer timer = new Timer();
            timer.schedule(timeoutTask,TIMEOUT_MILLS);
        }
    }

    private TimerTask timeoutTask = new TimerTask() {
        @Override
        public void run() {
            android.util.Log.d("lfy","[SmartRestartService] timeoutTask...");
//            alertDialog.dismiss();
        }
    };

    public void cancel(){
        timeoutTask.cancel();
    }
}
